package com.zyc.webtest.controller;

import com.zyc.webtest.domain.SysMenuEntity;
import com.zyc.webtest.service.SysMenuService;
import com.zyc.webtest.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by zyc on 2017/4/28.
 */
@Controller
@RequestMapping("sysmenu")
public class SysMenuController extends BaseController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public String list(@RequestParam Map<String, Object> params) {

        return "sysmenu/list" ;
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{menuId}")
    public String info(@PathVariable("menuId") Integer menuId) {
        SysMenuEntity sysMenu = sysMenuService.findById(menuId).orElse(new SysMenuEntity());

        return "sysmenu/info/menuId" ;
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ResponseBody
    public ResultMsg save(@RequestBody SysMenuEntity sysMenu) {
//        sysMenuService.save(sysMenu);

        return sysMenuService.save(sysMenu).map(u -> ResultMsg.success()).orElse(ResultMsg.error());
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ResponseBody
    public ResultMsg update(@RequestBody SysMenuEntity sysMenu) {

        return sysMenuService.save(sysMenu).map(u -> ResultMsg.success()).orElse(ResultMsg.error());
    }

    /**
     * 删除
     */
    @GetMapping("/delete/{menuId}")
//    @PostMapping("/delete/{menuId}")
    @ResponseBody
    public ResultMsg delete(@PathVariable("menuId") Integer menuId) {
        ResultMsg rm = null;
        try {
            sysMenuService.delete(menuId);
            rm = ResultMsg.success();
        } catch (Exception e) {
            logger.error("",e);
            rm = ResultMsg.error();
        }
        return rm;
    }
}
